<script setup>
import Vrouter from "@/router"
import { MYURL as URL } from '@/utils/express/express.js'
import { change_user_profile } from '@/api/user/user.js'
import { onMounted, ref, inject } from "vue";
import { get_user_info, change_user_info } from '@/api/user/user.js'
// import { emojiEncode, emojiDecode } from '@/utils/emoji/emoji.js'
const router = Vrouter
const getback = () => {
    router.go(-1)
}
const muself_img = ref(null)
const upload_image = ref(null)
const upload_state = ref(false)
const change_success = inject('change_message_state')
onMounted(() => {
    upload_image.value.addEventListener('change', async (e) => {
        if (!e.target.files) return alert('请上传文件')
        let file = e.target.files[0]
        const reader = new FileReader()
        reader.readAsDataURL(file)
        upload_state.value = true
        reader.onload = () => {
            muself_img.value.src = `${reader.result}`
            let fd = new FormData()
            fd.set("profile", file)
            change_user_profile(fd).then((res) => {
                upload_state.value = false
                change_success(res.data.mes)
            })
        }
    })
})
const keep_on = () => {
    change_user_info(info.value).then((res) => {
        change_success(res.data.mes)
    })
}
const now_count = ref(70)
const my_about = () => {
    now_count.value < 0 ? now_count.value = 0 : now_count.value = 70 - info.value.about_value.length
}
const info = ref({})
const sexCount = ref(0)
const CallCount = ref(0)
onMounted(() => {
    get_user_info().then((res) => {
        let person = res.data.mes[0]
        info.value.profile = `${URL}/public/profiles/${person.profile}`
        info.value.username = person.username
        switch (person.sex) {
            case 'man':
                info.value.sex = '男'
                sexCount.value = 0
                break;
            case 'gril':
                info.value.sex = '女'
                sexCount.value = 1
                break;

            case 'secret':
                info.value.sex = '保密'
                sexCount.value = 2
                break;
            default:
                info.value.sex = person.sex
                sexCount.value = 3
        }
        switch (person.system_call) {
            case '他':
                CallCount.value = 0
                break;
            case '她':
                CallCount.value = 1
                break;
            default:
                CallCount.value = 2
        }
        info.value.about_value = person.about
        info.value.systemcall = person.system_call
    })
})
const show_about = ref(false)
const change_sex = (num, sex) => {
    sexCount.value = num
    info.value.sex = sex
}
const change_about_state = () => {
    show_about.value = true
    my_about()
}
const showSex = ref(false)
const other_sex = ref(false)
const changeSystemCall = () => {
    CallCount.value >= 2 ? CallCount.value = 0 : CallCount.value++
    switch (CallCount.value) {
        case 0:
            info.value.systemcall = '他'
            break;
        case 1:
            info.value.systemcall = '她'
            break;
        default:
            info.value.systemcall = 'Ta'
    }
}
</script>

<template>
    <div class="back_color"></div>
    <div class="get-back-main">
        <img class="back-image" src="@/assets/img/getback.png" @click="getback">
        <div class="title">
            账号资料
        </div>
        <div class="keep-on" @click="keep_on">保存</div>
    </div>
    <div class="getback-placehoder"></div>
    <div class="detail-imformation">
        <div>
            <p>头像</p>
            <div class="img_conter">
                <img class="muself_img" :class="{ action: upload_state }" ref="muself_img" :src="info.profile">
                <p :class="{ showText: upload_state }">正在上传中</p>
            </div>
            <img src="@/assets/img/getback.png">
            <input type="file" name="" ref="upload_image" id="upload_image"
                accept="image/gif,image/jpeg,image/jpg,image/png">
        </div>
        <div class="change_name">
            <p>昵称</p>
            <input maxlength="10" v-model="info.username" type="text">
            <img src="@/assets/img/getback.png">
        </div>
        <div @click="showSex = !showSex" class="sex-select">
            <div class="sex_title">
                <p>性别</p>
                <div style="color: #9a9a9a;">
                    {{ info.sex }}
                    <img src="@/assets/img/getback.png">
                </div>
            </div>
            <div class="sex_all" :class="{ animal: showSex }">
                <div class="sex_select" @click.stop="change_sex(0, '男')">
                    <p>男</p>
                    <div :class="{ btnBackgournd: sexCount === 0 }" class="circleBoder"></div>
                </div>
                <div class="sex_select" @click.stop="change_sex(1, '女')">
                    <p>女</p>
                    <div :class="{ btnBackgournd: sexCount === 1 }" class="circleBoder"></div>
                </div>
                <div class="sex_select" @click.stop="change_sex(2, '保密')">
                    <p>保密</p>
                    <div :class="{ btnBackgournd: sexCount === 2 }" class="circleBoder"></div>
                </div>
                <div @click="other_sex = true" class="sex_select"
                    @click.stop="change_sex(3, sexCount === 3 ? info.sex : '其他')">
                    <p>其他</p>
                    <div :class="{ btnBackgournd: sexCount === 3 }" class="circleBoder"></div>
                </div>
            </div>
        </div>
        <div @click="change_about_state" class="change_name">
            <p>个性签名</p>
            <p>{{ info.about_value }}</p>
            <img src="@/assets/img/getback.png">
        </div>
    </div>
    <div class="show_about" v-show="show_about">
        <div>
            <textarea @input="my_about" name="" v-model="info.about_value" maxlength="70" id="" cols="30"
                rows="10"></textarea>
            <p class="text_count">{{ now_count }}</p>
            <p class="sure_btn" @click="show_about = false">确定</p>
        </div>
    </div>

    <div v-show="other_sex" class="other_sex" @click.self="other_sex = false">
        <div class="other_sex_main">
            <p class=other_title>其他选项</p>
            <div class="input_sex">
                <p>请输入您的性别:</p>
                <input type="text" v-model="info.sex" maxlength="5">
            </div>
            <div class="input_sex" @click="changeSystemCall">
                <p>系统指定您的人称代词为:</p>
                <p style="margin-left: 4px; color: black;">{{ info.systemcall }}</p>
            </div>
            <div class="sex_btn" @click="other_sex = false">完成</div>
        </div>

    </div>
</template>
    
<style scoped lang="less">
.other_sex {
    background-color: rgba(0, 0, 0, 0.30);
    height: 100vh;
    width: 100vw;
    position: fixed;
    z-index: 100;
    top: 0;
    left: 0;

    .other_sex_main {
        padding: 0 20px;
        position: fixed;
        bottom: 0;
        background-color: white;
        width: 100%;
        border-radius: 20px 20px 0 0;

        .other_title {
            margin-top: 10px;
            text-align: center;
            font-size: 18px;
        }

        .input_sex {
            margin-top: 20px;
            display: flex;
            font-size: 15px;
            padding: 10px 20px;
            border-radius: 10px;
            background-color: #f5f5f5;
            color: rgba(0, 0, 0, 0.70);

            p {
                white-space: nowrap;
            }

            input {
                margin-left: 4px;
                width: 100%;
                border: none;
                font-size: 14px;
                outline: none;
                background-color: #f5f5f5;
            }
        }

        .sex_btn {
            width: 100%;
            margin-top: 20px;
            margin-bottom: 20px;
            background-color: var(--bilibili-pink);
            color: #eff8ff;
            text-align: center;
            padding: 10px;
            border-radius: 20px;
        }
    }
}

.btnBackgournd {
    background-color: var(--bilibili-pink);
}

.showText {
    display: block !important;
}

.keep-on {
    position: fixed;
    right: 0;
    top: 0;
    transform: translate(-50%, 50%);
    color: #9a9a9a;
}

.sex-select {
    flex-direction: column;
    align-items: flex-start !important;
    padding-bottom: 15px !important;

    .sex_title {
        width: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .sex_all {
        width: 100%;
        transform: scale(0);
        height: 0;
        transition: .3s all;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
    }

    .animal {
        height: 140px;
        transform: scale(1);
    }

    .circleBoder {
        width: 15px;
        height: 15px;
        border: rgba(0, 0, 0, 0.20) solid 1px;
        border-radius: 50%;
    }

    .sex_select {
        padding-top: 10px;
        border-top: 1px solid rgba(0, 0, 0, 0.10);
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: space-between;
    }

    select,
    input {
        width: 100%;
        height: 100%;
        position: absolute;
        opacity: 0;
    }

    img {
        width: 8px;
        transform: rotate(180deg);
    }

}


.action {
    filter: brightness(30%);
}

.change_name {
    input {
        color: #9a9a9a;
        font-size: 14px;
        width: 70%;
        margin-left: auto;
        border: none;
        outline: none;
        text-align: end;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
}

.show_about {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background-color: rgba(0, 0, 0, 0.70);

    div {
        // border-radius: 100px;
        position: absolute;
        width: 100vw;
        height: 50vh;
        left: 50%;
        top: 30%;
        transform: translate(-50%, -50%);
        background-color: white;
        border-radius: 10px;

        textarea {
            border-radius: 10px;
            padding: 5px;
            font-size: 16px;
            width: 100%;
            height: 70%;
            border: none;
            outline: none;
            resize: none;
        }

        .text_count {
            position: absolute;
            color: #9a9a9a;
            right: 2%;
            bottom: 2%;
        }

        .sure_btn {
            background-color: var(--bilibili-pink);
            position: absolute;
            color: white;
            padding: 5px;
            border-radius: 10px;
            left: 2%;
            bottom: 2%;
        }
    }
}

.detail-imformation {
    margin-top: 10px;
    background-color: white;
    padding: 0 20px;

    >div:last-child {
        border: none;
    }

    #upload_image {
        position: absolute;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        opacity: 0;
    }

    >div {
        position: relative;
        padding: 20px 0;
        display: flex;
        align-items: center;
        gap: 8px;
        border-bottom: #eaeaea 1px solid;

        p {
            font-size: 14px;
        }

        >p:nth-child(2) {
            text-align: end;
            width: 70%;
            margin-left: auto;
            color: #9a9a9a;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }

        .img_conter {
            margin-left: auto;
            position: relative;

            p {
                display: none;
                font-size: 10px;
                position: absolute;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -50%);
                color: white;
                white-space: nowrap;
            }
        }

        .muself_img {
            border-radius: 50%;
            object-fit: cover;
            width: 60px;
            height: 60px;
        }

        img:nth-child(3) {
            width: 8px;
            transform: rotate(180deg);
        }
    }
}

.back_color {
    position: fixed;
    top: 0;
    left: 0;
    background-color: #f2f3f5;
    width: 100vw;
    height: 100vh;
    z-index: -1;
}
</style>
